<?php
namespace app\adminapiv3\controller\asset_manage\host;

/*
 * @Author: your name
 * @Date: 2020-06-05 17:32:41
 * @LastEditTime: 2020-12-28 10:17:01

 * @LastEditors: Please set LastEditors
 * @Description: 主机管理
 * @FilePath: /special/application/adminapiv3/controller/asset_manage/ServiceProviderService.php
 */


use app\adminapiv3\controller\BaseController;
use app\adminapiv3\service\asset_manage\host\HostBulkImport;
use app\adminapiv3\service\asset_manage\host\HostService;
use think\Request;


class HostController extends BaseController
{
    /**
     * @name: 数据统计
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-23 13:22:47
     */
    public function dataStatistics ()
    {
        $Host = new HostService();
        $data = $Host->statistics();

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-24 15:26:39
     */
    public function list (Request $request)
    {
        //接收参数
        $params = $request->only([
            'name','ip','os','tag','ministry','manager','online'
        ],'post');

        //根据资产组展示以及组id=0的数据
        $groupId = $this->getLoginHostGroupId();
        $params['group_id'] = $groupId;
        $HostService = new HostService();//公共

        $data = $HostService->list($params, true);
        return $this->jsonOutPut($data);
    }

    /**
     * @name: 主机详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-25 10:55:09
     */
    public function detail ($host_id)
    {
        $Host = new HostService();
        $data = $Host->detail($host_id);
        
        return $this->jsonOutPut($data);
    }


    /**
     * @name 网卡信息
     * @param Request $request
     * @return \think\response\Json
     * @author wx
     * @date 2020/10/28 15:20
     */
    public function network(Request $request)
    {
        $params = $request->only('host_id');
        $this->validate($params, 'Host.network');
        $server = new HostService();
        $info = $server->network($params['host_id']);
        return $this->jsonOutPut($info);
    }

    /**
     * @name: 添加主机信息 
     * @Param: 
     *  GET:
     *  POST: name:主机名; alias:主机别名;  ipAddress:主机ip地址; software: 业务系统名; os:系统id 1windows 2linux; ministry:所属部门名; manager:管理员名;
     * @Description: 
     * @Author: foo
     * @Date: 2019-03-22 17:59:09
     */
    public function add (Request $request)
    {
        $params = $request->only([
            'name',
            'ip',
            'type',
            'tag' => '',
            'software_id' => 0,
            'ministry_id' => 0,
            'manager_id' => 0,
            'develop_provider_id' => 0,
            'maintain_provider_id' => 0,
            'safe_provider_id' => 0,
            'address_id' => 0,
            'user' => '',
            'pwd' => '',
            'port' => ''
        ],'post');

        $this->validate($params, 'Host.add');

        $hostData = [
            'name' => $params['name'],
            'ip' => $params['ip'],
            'tag' => $params['tag'],
            'software_id' => $params['software_id'],
            'ministry_id' => $params['ministry_id'],
            'manager_id' => $params['manager_id'],
            'develop_provider_id' => $params['develop_provider_id'],
            'maintain_provider_id' => $params['maintain_provider_id'],
            'safe_provider_id' => $params['safe_provider_id'],
            'address_id' => $params['address_id'],
        ];

        $contactData = [
            'ip' => $params['ip'],
            'type' => $params['type'],
            'user' => $params['user'],
            'pwd' => $params['pwd'],
            'port' => $params['port'],
        ];

        $Host = new HostService();
        $data = $Host->add($hostData, $contactData);

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 11:24:36
     */
    public function modify (Request $request)
    {
        //接收参数
        $params = $request->only([
            'id',
            'name',
            'ip',
            'type',
            'tag' => '',
            'software_id' => 0,
            'ministry_id' => 0,
            'manager_id' => 0,
            'develop_provider_id' => 0,
            'maintain_provider_id' => 0,
            'safe_provider_id' => 0,
            'address_id' => 0,
        ],'post');

        $this->validate($params, 'Host.modify');

        $hostData = [
            'id' => $params['id'],
            'name' => $params['name'],
            'tag' => $params['tag'],
            'software_id' => $params['software_id'],
            'ministry_id' => $params['ministry_id'],
            'manager_id' => $params['manager_id'],
            'develop_provider_id' => $params['develop_provider_id'],
            'maintain_provider_id' => $params['maintain_provider_id'],
            'safe_provider_id' => $params['safe_provider_id'],
            'address_id' => $params['address_id'],
        ];

        $Host = new HostService();
        $Host->modify($hostData);

        return $this->jsonOutPut();
    }

    /**
     * @name: 单个/批量 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 13:16:46
     */
    public function delete (Request $request)
    {
        //接收参数
        $params = $request->only([
            'host_id',
            'is_recovery',
        ],'post');

        $this->validate($params, 'Host.delete');

        $Host = new HostService();
        $Host->uninstall($params['is_recovery']);

        //下发命令给客户端
        $this->sendCommandToClient('client', 'uninsatll', false);

        return $this->jsonOutPut([], true);

    }

    /**
     * @name: 主机硬件使用
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 14:25:03
     */
    public function hardwareUsed ($host_id)
    {
        $Host = new HostService();
        $data = $Host->hardware($host_id);

        return $this->jsonOutPut($data, true);

    }

    /**
     * @name: 检测
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-29 15:11:23
     */
    public function checkClient (Request $request)
    {
        //接收参数
        $params = $request->only([
            'ip',
            'type',
            'user' => '',
            'pwd' => '',
            'port' => '',
            ],'post');

        $this->validate($params, 'Host.ssh');
        $contactData = [
            'ip' => $params['ip'],
            'type' => $params['type'],
            'user' => $params['user'],
            'password' => $params['pwd'],
            'port' => $params['port'],
        ];



        $Host = new HostService();
        $Host->checkClient($contactData);

        return $this->jsonOutPut();
    }
    /**
     * @name: 筛选项
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-30 10:34:47
     */

    public function filterData ()
    {
        $Host = new HostService();
        $data = $Host->filterData();

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 端口列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 15:00:56
     */
    public function portList (Request $request, $host_id)
    {
        //接收参数
        $params = $request->only([
            'port' => '',
            'protocols' => '',
            'guise' => '',
            'host_id' => $host_id,
        ],'post');

        
        $HostPort = new HostService();
        $data = $HostPort->portList($params);

        return $this->jsonOutPut($data, true);
    }

    /**
     * @name: 批量导入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-07-27 14:41:30
     */
    public function bulkImport ()
    {
        $file = request()->file('file');
        $HostBulkImport = new HostBulkImport();
        $data = $HostBulkImport->import($file);

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 模版下载
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-07-27 14:42:46
     */
    public function template ()
    {
        $HostBulkImport = new HostBulkImport();
        $HostBulkImport->template();
        $data = [
            'url' => request()->domain() . '/template.xlsx'
        ];

        return $this->jsonOutPut($data);
    }

    /**
     * @name 客户端下载
     * @author wx
     * @date 2020/10/16 16:42
     */
    public function clientDownload(Request $request)
    {
        $type = $request->post('type');

        $server = new HostService();
        $data = $server->clientDownload($type);

        return $this->jsonOutPut($data);
    }

    /**
     * @name 展示所有资产组
     * @author wx
     * @date 2020/10/29 10:25
     */
    public function showGroup()
    {
        $server = new HostService();
        $list = $server->showGroup();

        return $this->jsonOutPut($list);
    }

    /**
     * @name 客户端绑定资产组
     * @param Request $request
     * @return \think\response\Json
     * @author wx
     * @date 2020/10/21 14:24
     */
    public function bindingGroup(Request $request)
    {
        $params = $request->only(['host_id', 'group_id']);

        $this->validate($params, 'Host.bindingGroup');

        $server = new HostService();
        $server->bindingGroup($params);

        return $this->jsonOutPut();
    }
}